<template>
  <el-dialog
    class="approve-invoicing-dialog"
    width="500px"
    :visible.sync="dialogVisible"
    title="审批开票申请"
    :close-on-click-modal="false"
    v-dialogDrag="true"
    @open="handleDialogOpen"
    @closed="handleDialogClosed"
  >
    <el-form
      ref="approveInvoicingForm"
      :model="formData"
      :rules="formRules"
      size="small"
      label-width="100px"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注:" prop="approveRemark" label-width="50px">
            <el-input
              type="textarea"
              rows="3"
              maxlength="100"
              show-word-limit
              v-model="formData.approveRemark"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="dialog-footer" slot="footer">
      <el-button style="margin-right: 50px;" type="primary" @click="handleSupple">补录开票信息</el-button>
      <el-button style="margin-right: 50px;" type="danger" @click="handleReject">驳回</el-button>
      <el-button @click="dialogVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { approveOper } from '@/api/person/invoicing'

export default {
  name: 'ApproveInvoicingDialog',
  components: {},
  dicts: [],
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    row: {
      type: Object,
      default: () => ({})
    },
  },
  computed: {
    dialogVisible: {
      get() {
        return this.show
      },
      set(value) {
        this.$emit('update:show', value)
      }
    }
  },
  data() {
    return {
      formData: {},
      formRules: {
        approveRemark: [],
      },
    }
  },
  created() {
    this.initFormData()
  },
  methods: {
    // 弹框打开时
    handleDialogOpen() {

    },
    // 弹框关闭时
    handleDialogClosed() {
      this.initFormData()
      this.$refs.approveInvoicingForm.resetFields()
    },
    // 初始化表单数据
    initFormData() {
      this.formData = {
        approveRemark: '',
      }
    },
    // 获取表单数据
    getFormData() {
      const data = JSON.parse(JSON.stringify(this.formData))
      data.invoiceApplyId = this.row.invoiceApplyId
      return data
    },
    // 点击补录
    handleSupple() {
      this.$emit('supple')
      this.dialogVisible = false
    },
    // 点击驳回
    handleReject() {
      this.$refs.approveInvoicingForm.validate(valid => {
        if (!valid) return
        let fn = approveOper
        const data = this.getFormData()
        data.approveState = false
        fn(data).then(res => {
          this.$message.success('操作成功')
          this.dialogVisible = false
          this.$emit('refresh')
        }).catch(err => {})
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.approve-invoicing-dialog {

  ::v-deep .el-dialog__body {
    max-height: 85vh;
    overflow-y: auto;
  }

  .line-1 ::v-deep .el-form-item__label {
    line-height: 1;
  }

  .append {
    position: absolute;
    right: 10px;
  }
}
</style>
